package com.net.shop.car.manager.ui.personalcenter.dingdan;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Dingdan;
import com.net.shop.car.manager.api.model.DingdanDetail;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.dingdan.GetDingdanOrderDetail;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanDetailResponse;
import com.net.shop.car.manager.ui.FullScreenDialog;
import com.net.shop.car.manager.ui.personalcenter.dingdan.NormalListAdapter.OnDingDanClickedListener;
import com.net.shop.car.manager.ui.view.AnimatedExpandableListView;
import com.net.shop.car.manager.utils.Constants.DingDanType;

public class DingdanOrderDialog extends FullScreenDialog implements OnDingDanClickedListener {

	/**
	 * 
	 */
	private DingDanType type;

	/**
	 * 
	 */
	private FrameLayout container;

	/**
	 * 
	 */
	private String[] dingdanArrayStrings;
	/**
	 * 
	 */
	private Map<Dingdan, DingdanDetail> details = new HashMap<Dingdan, DingdanDetail>();

	/**
	 * 
	 */
	private View detailView;
	/**
	 * 
	 */
	private int postType;

	/**
	 * 
	 */
	private View currentList;
	
	/**
	 * 
	 */
	private Member user;

	/**
	 * @param context
	 *            Context
	 */
	public DingdanOrderDialog(Context context, DingDanType type,Member user) {
		super(context, R.style.Dialog_Fullscreen);
		this.user = user;
		this.type = type;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dingdan_order);

		dingdanArrayStrings = activity.getResources().getStringArray(
				R.array.dingdan_name);

		initViews();
	}

	private void initViews() {
		String title = dingdanArrayStrings[type.value];

		View view = findViewById(R.id.dingdan_order_title);
		initTitle(view, title);

		view.findViewById(R.id.title_back_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		container = (FrameLayout) findViewById(R.id.dingdan_container);

		switch (type) {
		case vbmail:
			postType = 4;
			setNormalList();
			break;
		case outline:
			postType = 6;
			setNormalList();
			break;
		case online:
			postType = 5;
			setNormalList();
			break;
		case drawback:
			postType = -1;
			setExpandalList(type);
			break;
		case finish:
			postType = 2;
			setExpandalList(type);
			break;
		case oilcard:
			postType = -1;
			setExpandalList(type);
			break;
		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void setNormalList() {
		ListView listView = (ListView) activity.inflater.inflate(
				R.layout.listview, container, false);
		currentList = listView;
		container.addView(currentList);
		if (postType >= 0) {
			NormalListAdapter normalListAdapter = new NormalListAdapter(activity, postType,user,this);
			listView.setAdapter(normalListAdapter);
			listView.setOnItemClickListener(normalListAdapter);
		}
	}

	/**
	 * 
	 */
	private void setExpandalList(DingDanType danType) {
		AnimatedExpandableListView expandableListView = (AnimatedExpandableListView) activity.inflater
				.inflate(R.layout.animate_listview, container, false);
		currentList = expandableListView;
		container.addView(currentList);
		expandableListView.setGroupIndicator(null);
		ExpandListAdapter expandListAdapter = new ExpandListAdapter(activity,user, expandableListView, danType);
		expandableListView.setAdapter(expandListAdapter);
		expandableListView.setOnGroupExpandListener(expandListAdapter);
		expandableListView.setOnGroupClickListener(expandListAdapter);
	}
	
	@Override
	public void onBackPressed() {
		if (detailView != null) {
			container.removeView(detailView);
			detailView = null;
			container.addView(currentList);
		} else {
			super.onBackPressed();
		}
	}

	@Override
	public void onDingdanClicked(final Dingdan dingdan) {
		if (detailView != null) {
			return;
		}
		detailView = activity.inflater.inflate(R.layout.binarycode_bitmap_view, container, false);
		final BinaryCodeView binaryCodeView = new BinaryCodeView(detailView, activity);
		DingdanDetail detail = details.get(dingdan);
		if (detail == null) {
			activity.progressDialog.show();
			VolleyCenter.getVolley().addGetRequest(new GetDingdanOrderDetail(dingdan.getId()), 
					new VolleyListenerImpl<DingdanDetailResponse>(new DingdanDetailResponse()) {

						@Override
						public void deleveryResponse(
								DingdanDetailResponse response) {
							if(response.isSuccess()) {
								activity.progressDialog.dismiss();
								details.put(dingdan, response.getDetail());
								setViewData(binaryCodeView, response.getDetail());
							}
						}
					});
		} else {
			setViewData(binaryCodeView, detail);
		}
	}
	
	private void setViewData(BinaryCodeView binaryCodeView,DingdanDetail detail) {
		container.removeView(currentList);
		binaryCodeView.setData(detail, type);
		container.addView(detailView,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}
}
